const express = require('express');
const session = require('express-session');
var router = express.Router();
var bodyParser = require('body-parser');
const url = require('url');
const fs = require('fs');
// parse application/x-www-form-urlencoded
router.use(bodyParser.urlencoded({ extended: false }))
    // parse application/json
router.use(bodyParser.json())
    // router.use((req, res, next) => {
    //     res.setHeader('Content-type', 'text/html;charset=utf-8');
    //     next();
    // })
router.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 } }))
router.get('/index', (req, res) => {
    let title = 'jkw';
    let username = req.session.username;
    // let age = req.body.age;
    let age = url.parse(req.url, true).query.age;
    res.render('index', {
        title: title,
        username: username,
        age: age
    })
})
router.get('/login', (req, res) => {
    req.session.username = 'jjj';
    res.render('login', {});
})
router.get('/txt', (req, res) => {
    fs.readFile('../demo2/public/txts/model.txt', (err, data) => {
        if (err) {
            console.log(err);
            return;
        }
        console.log(data.toString());
    })
    res.send('11');
})

module.exports = router;